import {createAction, createThunkAction} from "../../lib/redux-utils";
import {get, postJson} from "../../lib/http";
import {getPrincipal} from "../../lib/identity";

const principal = getPrincipal();
export const showCreate = createAction("ORGANIZATION.SHOW_CREATE");
export const hideCreate = createAction("ORGANIZATION.HIDE_CREATE");
export const showCreateUser = createAction("ORGANIZATION.SHOW_CREATE_USER");
export const hideCreateUser = createAction("ORGANIZATION.HIDE_CREATE_USER");
export const showCreateAdminUser = createAction("ORGANIZATION.SHOW_CREATE_ADMIN_USER");
export const hideCreateAdminUser = createAction("ORGANIZATION.HIDE_CREATE_ADMIN_USER");
export const showModifyUser = createAction("ORGANIZATION.SHOW_MODIFY_USER");
export const hideModifyUser = createAction("ORGANIZATION.HIDE_MODIFY_USER");
export const showModifyPwd = createAction("ORGANIZATION.SHOW_MODIFY_PWD");
export const hideModifyPwd = createAction("ORGANIZATION.HIDE_MODIFY_PWD");
export const showModify = createAction("ORGANIZATION.SHOW_MODIFY");
export const hideModify = createAction("ORGANIZATION.HIDE_MODIFY");
export const showUsers = createAction("ORGANIZATION.SHOW_USERS");
export const hideUsers = createAction("ORGANIZATION.HIDE_USERS");

export const getAll = createThunkAction("ORGANIZATION.QUERY", params =>
    get("/organization/tree/" + params)
);
export const getAllOrganization = createThunkAction("ORGANIZATION.QUERY", () =>
    get("/organization/getTree")
);
export const getAllOrganization2 = createThunkAction("ORGANIZATION.QUERY", () =>
    get("/organization/getTree2/" + getPrincipal().id)
);
export const modify = createThunkAction("ORGANIZATION.MODIFY", params =>
    postJson("/organization/update", params)
);
export const create = createThunkAction("ORGANIZATION.CREATE", params =>
    postJson("/organization/create", params)
);
export const del = createThunkAction("", params =>
    get("/organization/delete/" + params + "/" + principal.id)
);
export const delUser = createThunkAction("", params =>
    get("/user/delete/" + params + "/" + principal.id)
);
export const getUsers = createThunkAction("ORGANIZATION.QUERY_USER", params =>
    get("/user/getOrgUsers/" + params)
);
export const createUser = createThunkAction("ORGANIZATION.CREATE_USER", params =>
    postJson("/user/create", params)
);
export const modifyUser = createThunkAction("ORGANIZATION.MODIFY_USER", params =>
    postJson("/user/update", params)
);
export const updatePwd = createThunkAction("ORGANIZATION.MODIFY_PWD", params =>
    postJson("/user/resetPwd", params)
);
export const getRoles = createThunkAction("ORGANIZATION.ROLES", () =>
    get("/role/allNoAdmin")
);
export const using = createThunkAction("ORGANIZATION.UNSING", params =>
    get("/organization/using/" + params)
);
export const unused = createThunkAction("ORGANIZATION.UNUSED", params =>
    get("/organization/unused/" + params)
);

